//
//  UITextViewExtendableViewController.m
//  iOS UI Sample Code
//
//  Created by 규성 이 on 12. 2. 20..
//  Copyright (c) 2012년 kkamci77@gmail.com. All rights reserved.
//

#import "UITextViewExtendableViewController.h"
#import "RNTextView.h"

@implementation UITextViewExtendableViewController
@synthesize rnTextView, bottomButton, rnRealTextView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.rnTextView setDelegate:self];
    self.rnRealTextView = [[[RNTextView alloc] initWithFrame:CGRectMake(10.0f, 115.0f, 300.0f, 30.0f)] autorelease];
    [self.rnRealTextView setTagOfTextView:10];
    [self.rnRealTextView setTextViewDelegate:self];
    [self.view addSubview:self.rnRealTextView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [rnTextView release];
    [bottomButton release];
    [rnRealTextView release];
    [super dealloc];
}

#pragma UITextView Delegate Implements
- (void)textViewDidChange:(UITextView *)textView
{
    if(textView.tag == 10)
    {
        if([textView contentSize].height <= 30.0f)
        {
            self.rnRealTextView.frame = CGRectMake(self.rnRealTextView.frame.origin.x, self.rnRealTextView.frame.origin.y, self.rnRealTextView.frame.size.width, 30.0f);
            [textView setScrollEnabled:NO];
        }
        else if([textView contentSize].height >= 100.0f)
        {
            [textView setScrollEnabled:YES];
        }else
        {
            self.rnRealTextView.frame = CGRectMake(self.rnRealTextView.frame.origin.x, self.rnRealTextView.frame.origin.y, self.rnRealTextView.frame.size.width, [textView contentSize].height);
            if([textView contentSize].height >= 80.0f)
            {
                [textView setScrollEnabled:YES];
            }else
            {
                [textView setScrollEnabled:NO];
            }
        }
    }
    else
    {
        if([textView contentSize].height <= 35.0f)
        {
            self.rnTextView.frame = CGRectMake(self.rnTextView.frame.origin.x, self.rnTextView.frame.origin.y, self.rnTextView.frame.size.width, 35.0f);
            [self.rnTextView setScrollEnabled:NO];
        }
        else if([textView contentSize].height >= 100.0f)
        {
            [self.rnTextView setScrollEnabled:YES];
        }else
        {
            self.rnTextView.frame = CGRectMake(self.rnTextView.frame.origin.x, self.rnTextView.frame.origin.y, self.rnTextView.frame.size.width, [textView contentSize].height);
            if([textView contentSize].height >= 80.0f)
            {
                [self.rnTextView setScrollEnabled:YES];
            }else
            {
                [self.rnTextView setScrollEnabled:NO];
            }
        }
        
        self.bottomButton.frame = CGRectMake(self.bottomButton.frame.origin.x, self.rnTextView.frame.origin.y + self.rnTextView.frame.size.height + 25.0f, self.bottomButton.frame.size.width, self.bottomButton.frame.size.height);
    //    self.rnTextView.frame.size.height = tvSize.height;
        //    [textView sizeToFit];
    }
}
@end
